package com.wbao.dianniu.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout.LayoutParams;

import com.alibaba.mobileim.conversation.YWMessage;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MarkerOptions.MarkerAnimateType;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.R;

public class MapLocActivity extends BaseActivity {
	private  ImageButton requestLocButton;
	MapView mMapView = null;
	private BaiduMap mBaiduMap = null;
	private LocationClient mLocationClient = null;
	private BDLocationListener myListener = new MyLocationListener();
	  private String cityStr = null ;
	  boolean isFirstLoc = true; // 是否首次定位
	    private MyLocationData locData;
	    /**
	     * 当前地点击点
	     */
	    private Marker mMarker;
	    BitmapDescriptor mCurrentMarker;
	    private LatLng latlng;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.activity_map_loc,LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//		setTitleName("活动地点");
		initview();
		initMap();
		showPosition();
	}
	private void initview() {
		requestLocButton = (ImageButton) findViewById(R.id.my_location_imagebutton);
	}
	
	private void initMap() {
		// 获取地图控件引用
		mMapView = (MapView) findViewById(R.id.bmapView);
		mBaiduMap = mMapView.getMap();
		// 开启定位图层
		mBaiduMap.setMyLocationEnabled(true);
		initLocation();
		requestLocButton.setOnClickListener(this);
		
	}
	
	private void showPosition() {
		Intent intent = getIntent();
		//******聊天点击位置跳转到此界面特殊处理****/// 
		int type = intent.getIntExtra(GlobalContext.INTENT_REPORT_TYPE, 0);
		if(type == YWMessage.SUB_MSG_TYPE.IM_GEO){
			setTitleName("位置");
		}else{
			setTitleName("活动地点");
		}
		//***********************************///
		
		double lat = intent.getDoubleExtra(GlobalContext.INTENT_MAP_LAT, 0);
		double lon = intent.getDoubleExtra(GlobalContext.INTENT_MAP_LONG, 0);
		latlng = new LatLng(lat, lon);
		if(0 != lat &&  0 != lon ){
			  mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding);
		    	MarkerOptions ooA = new MarkerOptions().position(latlng).icon(mCurrentMarker)
		    			.period(1)
		                .zIndex(2).draggable(true);
		        // 掉下动画
		        ooA.animateType(MarkerAnimateType.drop);
		       mMarker = (Marker) (mBaiduMap.addOverlay(ooA));
		       mMarker.setPosition(latlng);
			//显示当前位置
            MapStatus.Builder builder = new MapStatus.Builder();
            builder.target(latlng).zoom(18.0f);
            mBaiduMap .setMyLocationConfigeration(new MyLocationConfiguration(LocationMode.NORMAL, true, null));
            mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
		}
	}
	
	private void initLocation() {
		  // 定位初始化
		mLocationClient = new LocationClient(this);
		mLocationClient.registerLocationListener(myListener);
      LocationClientOption option = new LocationClientOption();
      option.setOpenGps(true); // 打开gps
      option.setCoorType("bd09ll"); // 设置坐标类型
      option.setScanSpan(1000);
      mLocationClient.setLocOption(option);
      mLocationClient.start();
	}
	public class MyLocationListener implements BDLocationListener {
		 
        @Override
        public void onReceiveLocation(BDLocation location) {
        	 // map view 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }
            cityStr = location.getCity();
             locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    .latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
            if (isFirstLoc) {
                isFirstLoc = false;
                LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
                MapStatus.Builder builder = new MapStatus.Builder();
                builder.target(ll).zoom(18.0f);
                mBaiduMap .setMyLocationConfigeration(new MyLocationConfiguration(LocationMode.NORMAL, true, null));
//             mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
            }
        }
	}
	
	@Override
	protected void onDestroy() {
	    // 退出时销毁定位
        mLocationClient.stop();
        mLocationClient.unRegisterLocationListener(myListener);
        // 关闭定位图层
        mBaiduMap.setMyLocationEnabled(false);
        mMapView.onDestroy();
        mMapView = null;
        super.onDestroy();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		// 在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
		mMapView.onResume();
		MobclickAgent.onResume(this);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		mMapView.onPause();
		MobclickAgent.onPause(this);
	}
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.my_location_imagebutton:
			 LatLng ll = new LatLng(locData.latitude,locData.longitude);
             MapStatus.Builder builder = new MapStatus.Builder();
             builder.target(ll).zoom(18.0f);
             mBaiduMap .setMyLocationConfigeration(new MyLocationConfiguration(LocationMode.NORMAL, true, null));
             mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
			break;
		default:
			break;
		}
	}
	@Override
	protected void onStop() {
		if(null != mLocationClient){
			mLocationClient.stop();
		}
		super.onStop();
	}
	
}
